﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;


namespace Host.Models
{
    public class ExceptionHelper
    {

        public static void CreateException(int fileLogId, string exceptionstring , int fileSourceId)
        {
            //FundId and User Id
            MembershipUser user = (MembershipUser)HttpContext.Current.Session["User"];//Get the user from the session object 
            FileExceptionLog exception = new FileExceptionLog();
            exception.FileLogId = fileLogId;
            exception.SourceId = fileSourceId;
            exception.LogUser = Security.PrincipalUser.LoggedInUser.UserName;
            exception.RaisedDateTime = DateTime.Now;
            exception.ExceptionDescription = exceptionstring;
            exception.FileLogId = fileLogId;
            if (HttpContext.Current.Session["FundId"] != null)
            {
                exception.FundId = Convert.ToInt32(HttpContext.Current.Session["FundId"]);
            }
            
            if (HttpContext.Current.Session["FundId"] != null)
            {
                exception.FileLogId = Convert.ToInt32(HttpContext.Current.Session["FileLogId"]);
            }


            using (Data db = new Data())
            {
                //db.FileExceptionLog.AddObject(new FileExceptionLog{FileLogId = fileLogId, SourceId = fileSourceId, ExceptionDescription = exceptionstring, RaisedDateTime = DateTime.Now});
                db.FileExceptionLog.AddObject(exception);
                db.SaveChanges();
            }
        }

        


        public static void ValidateClaims(List<ClaimImportModel> data, int fundId)
        {
            //Get Claim Rules
            //Bennifit claims = 8

            List<Question> questions = new Question().GetQuestions(8);
            var qa = new FundAnswerSheet().GetForFund(fundId);
        }

    }

    public partial class Question
    {
        

        public int? DataTypeId { get; set; }

        

        public string GetDataType(int dataTypeId)
        {
            return new Data().AnswerDataType.Where(x => x.AnswerDataTypeId == dataTypeId)
                    .Select(i=> i.AnswerDataTypeDescription)
                .ToString();
        }

        public List<Question> GetQuestions(int questionGroupId)
        {
            return new Data().Question
                .Where(x => x.QuestionGroupId == questionGroupId)
                .Select(i => new Question
                                 {
                                     QuestionId = i.QuestionId,
                                     DataTypeId = i.AnswerDataTypeId,
                                     AnswerMaxLength = i.AnswerMaxLength
                                 }).ToList();
        }
    }

    public class ValidateQA
    {

        public bool ValidateAnswer(int questionId,string questionType, string exceptedAnswer, string currentValue, int importTypeId)
        {
            bool result = false;
            var q = new Data().Question.Where(i => i.QuestionId == questionId).Select(x => x.Question1).
                FirstOrDefault();
            var e = q + " -Answer is incorrect - current value: " + currentValue + " - expected : " + exceptedAnswer;
            try
            {
            switch (questionType)
            {
                case "Boolean":
                    bool expectedA = Convert.ToBoolean(exceptedAnswer);
                    bool currentA = Convert.ToBoolean(currentValue);
                    if (expectedA == currentA)
                    {
                        result = true;
                    }
                    else
                    {
                        ExceptionHelper.CreateException(0,e , importTypeId);
                    }
                    break;
                case "String":
                    if (exceptedAnswer == currentValue)
                    {
                        result = true;
                    }
                    else
                    {
                        ExceptionHelper.CreateException(0, e, importTypeId);
                    }
                    break;
                case "Integer":
                    int expA = Convert.ToInt32(exceptedAnswer);
                    int curA = Convert.ToInt32(currentValue);
                    if (expA == curA)
                    {
                        result = true;
                    }
                    else
                    {
                        ExceptionHelper.CreateException(0, e, importTypeId);
                    }
                    break;
                case "Numeric":
                    Decimal expDecimalA = Convert.ToDecimal(exceptedAnswer);
                    Decimal curDecimalA = Convert.ToDecimal(currentValue);
                    if (expDecimalA == curDecimalA)
                    {
                        result = true;
                    }
                    else
                    {
                        ExceptionHelper.CreateException(0, e, importTypeId);
                    }
                    break;
            }
            }
            catch (System.Exception ex)
            {
                ExceptionHelper.CreateException(0, ex.Message, importTypeId);

            }
            return result;
        }

    }

    public partial class FundAnswerSheet
    {

        public string Answer { get; set; }

        public IEnumerable<FundAnswerSheet> GetForFund(int fundId)
        {
            return new Data().FundAnswerSheet
                .Where(i => i.FundId == fundId)
                .Select(x => new FundAnswerSheet
                                 {
                                     QuestionId = x.QuestionId,
                                     Answer = x.AnswerValue
                                 }).ToList();
        }

    }
    
}